#include "stdafx.h"
#include "choice_of_parameters.h"
#include "parameter_editor.h"
#include "units.h"
using namespace std;

ChoiceOfParameters::ChoiceOfParameters(QWidget* pwgt) 
	: QDialog(pwgt, Qt::WindowTitleHint | Qt::WindowSystemMenuHint)
{
	ui.setupUi(this);
	connect(ui.btnPlot, SIGNAL(clicked), this, SLOT(on_btnPlot_clicked));
	connect(ui.btnCSV, SIGNAL(clicked), this, SLOT(on_btnCSV_clicked));
	connect(ui.cbAllParameters, SIGNAL(stateChanged(int)), this, SLOT(on_cbAllParameters_changed(int)));
	ui.cbAllParameters->setTristate(false);
	//ui.table->setStyleSheet("alternate-background-color: rgb(190, 234, 235)");
	ui.table->setItemDelegate(new LineEditDelegate);
	ui.table->horizontalHeader()->setResizeMode(0, QHeaderView::Fixed);
	
	ui.table->setColumnWidth(0, 18);
	ui.table->setColumnWidth(1, 120);
	ui.table->setColumnWidth(2, 60);
/*
	ui.table->setHorizontalHeaderItem(0, new QTableWidgetItem("C"));
	ui.table->setHorizontalHeaderItem(1, new QTableWidgetItem("Name"));
	ui.table->setHorizontalHeaderItem(2, new QTableWidgetItem("Unit"));
	ui.table->setHorizontalHeaderItem(3, new QTableWidgetItem("Description"));*/
}

void ChoiceOfParameters::on_btnPlot_clicked(){
	isPlot = true;
	accept();
}

void ChoiceOfParameters::on_btnCSV_clicked(){
	isPlot = false;
	accept();
}

void ChoiceOfParameters::on_btnReject_clicked(){
	int result = QMessageBox::information(this, "Close", 
		"Close the form? any unsaved data will be lost", 
		QMessageBox::Yes | QMessageBox::Default, 
		QMessageBox::No, 
		QMessageBox::Cancel | QMessageBox::Escape); 
	if (result == QMessageBox::Yes) 
		reject();
}

void ChoiceOfParameters::setData(const QStringList &parameters, /*const QStringList &units,*/ const QStringList &descriptions){
	ui.table->clear();
	parameter.clear();
	unit.clear();
	check.clear();
	description.clear();
	ui.table->setRowCount(parameters.size());
	Units *unitlist = new Units();
	for (int i = 0; i < parameters.size(); i++){
		//check
		QCheckBox *cb = new QCheckBox;
		connect(cb, SIGNAL(stateChanged(int)), this, SLOT(on_check_changed(int)));
		check.push_back(cb);
		ui.table->setCellWidget(i, 0, cb);
		//name
		QLabel *name = new QLabel;
		name->setText(parameters[i]);
		parameter.push_back(name);
		ui.table->setCellWidget(i, 1, name);
		//unit
		QComboBox *u = new QComboBox;
		QStringList units = unitlist->getUnitListForParameter(parameters[i]);
		for (int j = 0; j < units.size(); j++)
			u->addItem(units[j]);
		int default_unit_index = 0;//getDefaultUnitIndex(parameters[i]);
		if (u->count() >= default_unit_index) u->setCurrentIndex(default_unit_index);
		unit.push_back(u);
		ui.table->setCellWidget(i, 2, u);
		//description
		QLabel *lbl = new QLabel;
		lbl->setText(i < descriptions.size() ? descriptions[i] : "-");
		description.push_back(lbl);
		ui.table->setCellWidget(i, 3, lbl);
	}

}

pair<QStringList, QStringList> ChoiceOfParameters::getData(){
	QStringList parameter_name, parameter_unit;
	for(int i = 0; i < parameter.size(); ++i)
		if (check[i]->checkState() == Qt::Checked) {
			parameter_name.push_back(parameter[i]->text());
			parameter_unit.push_back(unit[i]->currentText());
		}
	return make_pair(parameter_name, parameter_unit);
}

void ChoiceOfParameters::on_cbAllParameters_changed(int value){
	if (value == 1) return;
	foreach(QCheckBox *cb, check)
		cb->setCheckState((Qt::CheckState)value);
}

void ChoiceOfParameters::on_check_changed(int value){
	int countChecked = 0, countUncheched = 0, index = 0;
	
	while (index < check.size() && (countChecked == 0 || countUncheched == 0) ){
		if (check[index]->checkState() == Qt::Unchecked)
			countUncheched++;
		else
			countChecked++;
		index++;
	}
	if (countChecked != 0 && countUncheched != 0){
		ui.cbAllParameters->setCheckState(Qt::PartiallyChecked);
	}else if (countChecked == 0){
		ui.cbAllParameters->setCheckState(Qt::Unchecked);
	}else if (countUncheched == 0){
		ui.cbAllParameters->setCheckState(Qt::Checked);
	}
	ui.btnPlot->setEnabled(countChecked);
	ui.btnCSV->setEnabled(countChecked);
}